package com.xt.core.helper.page;

import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * a help class for pager properties in action
 * can be used in modeldriven action
 * contains commen properties for pagenation, for example
 * 1. sort
 * 2. search query
 * 3. pager 
 * 
 * url must be like list.action?currentPage=3&pagerMethod=first
 * 
 * <s:url value="list.action" id="firstpage">
                <s:param name="currentPage" value="currentPage"/>
                <s:param name="pagerMethod" value="'first'"/>
            </s:url>
            <s:url value="list.action" id="previouspage">
                <s:param name="currentPage" value="currentPage"/>
                <s:param name="pagerMethod" value="'previous'"/>
            </s:url>
            <s:url value="list.action" id="nextpage">
                <s:param name="currentPage" value="currentPage"/>
                <s:param name="pagerMethod" value="'next'"/>
            </s:url>
			<s:url value="list.action" id="lastpage">
                <s:param name="currentPage" value="currentPage"/>
                <s:param name="pagerMethod" value="'last'"/>
   </s:url>
 * 
 * @author txiao
 * @version Revision: 1.0, Date: 02.11.2009
 */
public class PageBean {

	protected final Logger log = LoggerFactory.getLogger(getClass());
	
	private int totalRows;
	private String currentPage;
	private String pagerMethod;
	private Pager pager;
	private LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
	private LinkedHashMap<String, String> queryParams = new LinkedHashMap<String, String>();
	private String page; //open the page directly 
	
	public PageBean(){
	
	}
	
	public PageBean(int totalRows){
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(currentPage, pagerMethod, totalRows);
		this.currentPage = String.valueOf(pager.getCurrentPage());
	}
	
	public PageBean(int totalRows, int pageSize){
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(currentPage, pagerMethod, totalRows);
		this.pager.setPageSize(pageSize);
		this.currentPage = String.valueOf(pager.getCurrentPage());
	}
	
	public void refresh(int totalRows){
		//log.debug("pageform refresh: totalrows: " + totalRows + " currentPage: " + currentPage + " method: " + pagerMethod);
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(currentPage, pagerMethod, totalRows);
		//we must refresh the current page
		this.currentPage = String.valueOf(pager.getCurrentPage());
		//log.debug("pageform refresh: totalPages: " + pager.getTotalPages() + " next page: " + pager.getCurrentPage());
	}
	
	public void reload(int totalRows){
		//log.debug("pageform refresh: totalrows: " + totalRows + " currentPage: " + currentPage + " method: " + pagerMethod);
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(page, totalRows);
		//we must refresh the current page
		this.currentPage = String.valueOf(pager.getCurrentPage());
		//log.debug("pageform refresh: totalPages: " + pager.getTotalPages() + " next page: " + pager.getCurrentPage());
	}
	
	
	/**
	 * for the case pagesize from the user
	 * @param totalRows
	 * @param pageSize
	 */
	public void refresh(int totalRows, int pageSize){
		//log.debug("pageform refresh: totalrows: " + totalRows + " currentPage: " + currentPage + " method: " + pagerMethod);
		this.totalRows = totalRows;
		this.pager = PagerService.getPager(currentPage, pagerMethod, totalRows);
		this.pager.setPageSize(pageSize);
		//we must refresh the current page
		this.currentPage = String.valueOf(pager.getCurrentPage());
		//log.debug("pageform refresh: totalPages: " + pager.getTotalPages() + " next page: " + pager.getCurrentPage());
	}
	
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getTotalRows() {
		return totalRows;
	}	
	public String getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	public String getPagerMethod() {
		return pagerMethod;
	}
	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}
	public Pager getPager() {
		return pager;
	}
	public LinkedHashMap<String, String> getOrderby() {
		return orderby;
	}
	public void setOrderby(LinkedHashMap<String, String> orderby) {
		this.orderby = orderby;
	}
	public void addOrder(String name, String value){
		this.orderby.put(name,value);
	}
	public LinkedHashMap<String, String> getQueryParams() {
		return queryParams;
	}
	public void setQueryParams(LinkedHashMap<String, String> queryParams) {
		this.queryParams = queryParams;
	}
	public void addQureyParam(String name, String value){
		this.queryParams.put(name, value);
		
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
}
